/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import db.JDBCUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class CustomerInfoDaoImpl implements CustomerInfoDao{

    public static void main(String[] args){
        List<CustomerInfo> list=new CustomerInfoDaoImpl().displayAllCustomerInfo();
        System.out.println("NYF");
    }
    @Override
    public List<CustomerInfo> displayAllCustomerInfo() {
        Connection conn=null;
        Statement stat=null;
        ResultSet rs=null;
        String sql="select * from customerinfo";
        List<CustomerInfo> list=new ArrayList<CustomerInfo>();
        try{
            conn=JDBCUtil.getConnection();
            stat=conn.createStatement();
            rs=stat.executeQuery(sql);
            while(rs.next()){
                CustomerInfo customer=new CustomerInfo();
                customer.setCID(rs.getInt("CID"));
                customer.setCustomerName(rs.getString("CustomerName"));
                customer.setCustomerTel(rs.getString("CustomerTel"));
                customer.setCustomerAddress(rs.getString("CustomerAddress"));
                customer.setCustomerDesc(rs.getString("CustomerDesc"));
                list.add(customer);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtil.realeaseAll(rs, stat, conn);
        }
        return list;
    }

    @Override
    public int updateCustomerInfo(CustomerInfo customer) {
        String sql="UPDATE customerinfo SET CustomerName=?, CustomerTel=?, CustomerAddress=?, CustomerDesc=? where CID=?";
        Connection conn=null;
        PreparedStatement pstat=null;
        ResultSet rs=null;
        
        int count=0;
        try{
            conn=JDBCUtil.getConnection();
            pstat=conn.prepareCall(sql);
            
            pstat.setString(1, customer.getCustomerName());
            pstat.setString(2, customer.getCustomerTel());
            pstat.setString(3, customer.getCustomerAddress());
            pstat.setString(4, customer.getCustomerDesc());
            pstat.setInt(5,customer.getCID() );
            count=pstat.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtil.realeaseAll(rs, pstat, conn);
        }
        return count;
    }
    @Override
        public int inputCustomerInfo(CustomerInfo customer) {
        String sql="insert  into customerinfo(CID,CustomerName,CustomerTel,CustomerAddress,CustomerDesc) values(?,?,?,?,?)";
        Connection conn=null;
        PreparedStatement pstat=null;
        ResultSet rs=null;
        
        int count=0;
       try{
            conn=JDBCUtil.getConnection();
            pstat=conn.prepareCall(sql);
            pstat.setInt(1, customer.getCID());
            pstat.setString(2, customer.getCustomerTel());
            pstat.setString(3, customer.getCustomerAddress());
            pstat.setString(4, customer.getCustomerDesc());
            pstat.setString(5, customer.getCustomerDesc());
            count=pstat.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtil.realeaseAll(rs, pstat, conn);
        }
        return count;
    }
    
}
